import 'package:flutter/material.dart';
import 'package:flutter_shop/pages/base/base_state_less_widget.dart';
import 'package:flutter_shop/pages/common/login/entity/user_info.dart';
import 'package:flutter_shop/pages/common/login/provider/login_provider.dart';
import 'package:flutter_shop/pages/member/member_tab_page/member_tab_presenter.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:flutter_shop/widget/load_image.dart';
import 'package:flutter_shop/widget/water_click.dart';
import 'package:provider/provider.dart';

/// Title:顶部头像 <br/>
/// Description: <br/>
/// author:Administrator <br/>
/// date: 2020-03-11  <br/>
/// version 1.0 <br/>
class TopHeader extends BaseStateLessWidget<MemberTabPresenter> {
  @override
  Widget build(BuildContext context) {
    UserInfo userInfo = Provider.of<LoginProvider>(context).userInfo;

    return Container(
      width: double.infinity,
      height: 220,
      decoration: BoxDecoration(
        color: CommonColors.primary,
        image: DecorationImage(
          image: AssetImage(R.assetsImgMemberImgHeaderBg),
          repeat: ImageRepeat.repeat,
        ),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Gaps.v_gap_15,
          Gaps.v_gap_15,
          Container(
            width: 90,
            height: 90,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(45),
              border: Border.all(width: 2, color: Colors.white),
            ),
            child: _headImage(userInfo),
          ),
          _nameWidget(userInfo),
        ],
      ),
    );
  }

  ///用户头像
  Widget _headImage(UserInfo userInfo) {
    Widget headImage = Image.asset(
      R.assetsImgMemberIconDefaultHeader,
      width: 90,
      height: 90,
      fit: BoxFit.cover,
    );
    if (userInfo != null && Utils.strIsNotEmpty(userInfo.headPic)) {
      headImage = InkWell(
        onTap: () {
          mPresenter.goPersonInfo();
        },
        child: LoadImage(
          userInfo.headPic,
          errorImg: R.assetsImgMemberIconDefaultHeader,
          width: 90,
          height: 90,
          fit: BoxFit.cover,
          isClipOval: true,
        ),
      );
    }
    return headImage;
  }

  ///用户昵称
  Widget _nameWidget(UserInfo userInfo) {
    Widget nameWidget = WaterClick(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2),
      margin: EdgeInsets.only(top: 20),
      decoration: BoxDecoration(
        color: CommonColors.primary,
        borderRadius: BorderRadius.circular(5),
        border: Border.all(width: 1, color: Colors.white),
      ),
      onPressed: () {
        //去登录
        mPresenter.goLogin();
      },
      child: Text(
        "注册/登录",
        style: TextStyle(color: Colors.white, fontSize: CommonDimens.font_size_small),
      ),
    );

    if (userInfo != null && Utils.strIsNotEmpty(userInfo.username)) {
      nameWidget = Text(
        userInfo.username,
        style: TextStyles.white_text,
      );
    }
    return nameWidget;
  }
}
